<template>
  <component
    ref="workTickets"
    :is="componentsName"
    @goWorkOrder="goWorkOrder"
    @previewTicket="previewTicket"
    @goOperationTicket="goOperationTicket"
    @goChangeTicket="goChangeTicket"
    @deleteTicket="deleteTicket"
    @updateListData="updateListData"
    @gotoDetails="gotoDetails"
  >
  </component>
</template>
<script>
import ThirdTicket from "./third";

import {
  getWorkTicketInfo,
  deleteWorkTicket,
} from "@/api/twoTicketManagement/workTicket.js";

export default {
  components: {
    ThirdTicket,
  },
  data() {
    return {
      componentsName: "", // 工作票种类英文
    };
  },
  created() {
    let ticketType = this.$route.query.ticketType;
    if (ticketType) {
      this.componentsName = ticketType; // 工作票类型type与组件名称一一对应
    } else {
      let id = this.$route.query.val;
      this.setTicketType(id);
    }
  },
  methods: {
    // 查询设置工作票类型
    async setTicketType(id) {
      let appCode = this.$store.getters.currentAppInfo.appCode;
      let data = { id: id };
      let res = await getWorkTicketInfo(data);
      if (res.data.type == "ElectricalFirst" && appCode == "EAM_YCSC") {
        this.componentsName = "ElectricalFirst_YC"; // 工作票类型type与组件名称一一对应
      } else {
        this.componentsName = res.data.type; // 工作票类型type与组件名称一一对应
      }
    },
    // 打开工单(工单id,工单类型)
    goWorkOrder(orderId, orderType) {
      if (orderId) {
        if (orderType == "repair") {
          // 计划检修工单
          let { href } = this.$router.resolve({
            path: "/orderManagement/planRepair/details",
            query: {
              id: orderId,
              pageType: "edit",
            },
          });
          window.open(href, "_blank");
        } else if (orderType == "defect") {
          // 缺陷单
          let { href } = this.$router.resolve({
            path: "/orderManagement/defectOrder/details",
            query: {
              id: orderId,
              pageType: "edit",
            },
          });
          window.open(href, "_blank");
        } else if (orderType == "maintain") {
          // 正常维护工单
          let { href } = this.$router.resolve({
            path: "/orderManagement/normalMaintenance/details",
            query: {
              id: orderId,
              pageType: "edit",
            },
          });
          window.open(href, "_blank");
        } else if (orderType == "overhaul") {
          // 大修工单
          let { href } = this.$router.resolve({
            path: "/orderManagement/overhaul/details",
            query: {
              id: orderId,
              pageType: "edit",
            },
          });
          window.open(href, "_blank");
        } else if (orderType == "experiment") {
          //试验工单
          let { href } = this.$router.resolve({
            path: "/orderManagement/experimentOrder/details",
            query: {
              id: orderId,
              pageType: "edit",
            },
          });
          window.open(href, "_blank");
        }
      }
    },
    // 预览
    previewTicket(url) {
      let data =
        url +
        `&token=${
          localStorage.getItem("GDH_WATER_TOKEN") || process.env.VUE_APP_TOKEN
        }&dynamicDataSource=${localStorage.getItem(
          "currDs"
        )}&dynamicOrg=${localStorage.getItem(
          "currOrg"
        )}&dynamicLeader=${localStorage.getItem(
          "currLeader"
        )}&dynamicLeaderOrg=${localStorage.getItem("currLeaderOrg")}`;
      this.$utils.isInstallPrintSoftware(data);
    },
    // 申请操作票
    goOperationTicket(data, type) {
      let href = "";
      if (data.id == 1) {
        href = this.$router.resolve({
          name: "substationTicketDetails",
          query: {
            pageType: data.pageType,
            id: data.workTicketId,
            operateType: data.operateType,
          },
        }).href;
      } else {
        href = this.$router.resolve({
          name: "dispatchTicketDetails",
          query: {
            pageType: data.pageType,
            id: data.workTicketId,
            operateType: data.operateType,
          },
        }).href;
      }
      window.open(href, "_blank");
    },
    gotoDetails(data, type = '') {
      let href;
      if (data.group == "factory") {
        href = this.$router.resolve({
          name: "substationTicketDetails",
          query: { pageType: data.pageType, id: data.id },
        }).href;
      } else {
        href = this.$router.resolve({
          name: "dispatchTicketDetails",
          query: { pageType: data.pageType, id: data.id },
        }).href;
      }
      if(type === 'zsj') {
        href = this.$router.resolve({
          name: "zsjSubstationTicketDetails",
          query: {
            pageType: data.pageType,
            parentId: data.workTicketId,
            id: data.workTicketId,
            operateType: data.operateType,
          },
        }).href;
      }
      window.open(href, "_blank");
    },
    // 变更工作票
    goChangeTicket(item) {
      let { href } = this.$router.resolve({
        name: "workTicketChangeDetails",
        query: {
          formData: item ? JSON.stringify(item) : "",
          pageType: "changeTicket",
        },
      });
      window.open(href, "_blank");
    },
    // 删除工作票
    deleteTicket(val) {
      if (val.id) {
        this.$confirm(`是否确认删除`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteWorkTicket(val).then(async (res) => {
              if (res.code == 200) {
                this.$message.success("删除成功");
                this.updateListData();
                setTimeout(() => {
                  window.close();
                }, 1000);
              }
            });
          })
          .catch((e) => {});
      } else {
        this.$message.warning("当前工作票没有保存。");
      }
    },
    /* 更新列表页的数据 */
    updateListData() {
      /* 通过localStorage来实现浏览器不同窗口之间的通信 */
      let num = Math.floor(Math.random() * 1000) + 1; //产生随机数，保证每次的值都不一样
      localStorage.setItem("update", num); //设置localStorage里面的值，保证每次都会触发storage事件
    },
  },
};
</script>
<style lang="less" scoped></style>
